﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmQueryDevice : FrmBase
    {
        private string targetForm = "";
        private object parentInfo = null;
        private ScanService scanService;
        private DeviceService deviceService = new DeviceService();
        private string title = "";

        public FrmQueryDevice()
        {
            InitializeComponent();
            ttlbrhead.Text = "设备查找";
            ilstdown.Images.Add(ResourceImage.确定02);
            ilstdown.Images.Add(ResourceImage.键盘02);
            ilstdown.Images.Add(ResourceImage.返回02);
            ilstbcup.Images.Add(ResourceImage.确定01);
            ilstbcup.Images.Add(ResourceImage.键盘01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            btnSelectDeviceType.UpImage = ResourceImage.设备类型01;
            btnSelectDeviceType.DownImage = ResourceImage.设备类型02;
            ibtnscanner.UpImage = ResourceImage.使用扫描仪01;
            ibtnscanner.DownImage = ResourceImage.使用扫描仪02;
        }

        private void FrmQueryDevice_Load(object sender, EventArgs e)
        {
            scanService = new ScanService(this);
            scanService.OnReadCode += scanService_OnReadCode;

            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbTitle.Text = title;
        }

        private void FrmQueryDevice_Closed(object sender, EventArgs e)
        {
            scanService.Close();

        }


        void scanService_OnReadCode(string code)
        {
            tbDeviceCode.Text = code;
            QueryDevice();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            if (PriorForm == "FrmEquipLst")
            {
                //从设备类型选择返回
                tbDeviceType.Text = (string)Parameter[0];

                //如果是从设备类型选择返回，需要读取之前的信息
                object[] oldParameters = (object[])(Parameter[1]);
                parentInfo = oldParameters[0];
                targetForm = (string)oldParameters[1];
                title = (string)oldParameters[2];
                tbDeviceName.Text = (string)oldParameters[3];
                tbDeviceCode.Text = (string)oldParameters[5];
            }
            else
            {
                title = (string)Parameter[0];
                parentInfo = Parameter[1];
                object[] parentParameters = (object[])(parentInfo);
                targetForm = (string)parentParameters[0];
            }
 
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            title = (string)Caches[0];
            targetForm = (string)Caches[1];
            tbDeviceName.Text = (string)Caches[2];
            tbDeviceType.Text = (string)Caches[3];
            tbDeviceCode.Text = (string)Caches[4];
        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {

                case 0: //确定
                    QueryDevice();
                    break;
                case 1: //键盘
                    InputMethod.SHFullScreen(this.Handle, 4);
                    InputMethod.SipShowIM(1);
                    tbDeviceName.Focus();
                    break;
                case 2: //返回
                    FormManage.GotoNext(targetForm, null, null, true);
                    break;
            }
        }

        private void QueryDevice()
        {
            List<Device> devices = deviceService.GetDevices(tbDeviceCode.Text, tbDeviceName.Text, tbDeviceType.Text);

            if (devices.Count == 0)
            {
                mssdlgComfirm.ShowDialog(this, "没有符合条件的设备", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (devices.Count > 1)
            {
                FormManage.GotoNext("FrmSelectDevice", new object[] {parentInfo, devices }, new object[] {title,targetForm, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text }, false);
            }
            else if (devices.Count == 1)
            {
                FormManage.GotoNext(targetForm, new object[] { devices[0],parentInfo }, new object[] {title,targetForm, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text }, false);
            }
        }

        private void btnSelectDeviceType_Click(object sender, EventArgs e)
        {
            FormManage.GotoNext("FrmEquipLst", new object[] { "FrmQueryDevice", new object[] { parentInfo,targetForm,title, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text } }, new object[] { title,targetForm, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text }, false);

        }

        private void ibtnscanner_Click(object sender, EventArgs e)
        {
            scanService.Scan();
        }

        private void FrmQueryDevice_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "194")
            {
                scanService.Scan();
            }
        }




    }
}